package com.blast.comicsisland.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import com.blast.comicsisland.R;
import com.blast.comicsisland.adapters.GuideAdapter;
import com.blast.comicsisland.model.Constants;

import java.util.LinkedList;
import java.util.List;

/**
 * 引导页
 */
public class GuideActivity extends ActionBarActivity{

    private ViewPager pager;
    private List<Integer> imageIds;
    private android.os.Handler handler = new android.os.Handler(){

        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what){
                case 998:
                    Intent intent = new Intent(GuideActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        pager = (ViewPager) findViewById(R.id.guide_view_pager);

        imageIds = new LinkedList<>();
        imageIds.add(R.drawable.help1);
        imageIds.add(R.drawable.help2);
        imageIds.add(R.drawable.help3);
        imageIds.add(R.drawable.help4);
        GuideAdapter adapter = new GuideAdapter(this,imageIds,handler);

        pager.setAdapter(adapter);

        PackageManager manager = getPackageManager();

        int versionCode = 1;

        try {
            PackageInfo packageInfo = manager.getPackageInfo(getPackageName(),PackageManager.GET_ACTIVITIES);

            versionCode = packageInfo.versionCode;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        SharedPreferences preferences = getSharedPreferences(Constants.SP_NAME_APP,MODE_PRIVATE);

        SharedPreferences.Editor edit = preferences.edit();

        edit.putBoolean(Constants.SP_SHOW_KEY_GUIDE + versionCode,true);

        edit.commit();

    }
}
